// pages/message/message.js
var app = getApp()
let api = require('../../../api/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal:false,
    region: [],
    customItem: '全部'
  },

  // 更换头像
  message_photo:function(){
    var _this = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        _this.setData({
          head: res.tempFilePaths[0]
        })
        // wx.uploadFile({
        //   url: 'http://www.900yeah.com/api/dblong/updateuser',
        //   filePath: res.tempFilePaths[0],
        //   name: 'img',
        //   formData: {
        //     id: _this.data.content.id,
        //     type:1
        //   },
        //   success(res) {
        //     console.log(res)
        //   }
        // })
      }
    })
  },
  bindRegionChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  qiehuan: function () {
    wx.reLaunch({
      url: '/pages/verify/verify',
    })
  },

  address:function(e){
    var content = this.data.content
    content.address = e.detail.value
    this.setData({
      content:content
    })
  },
  url:function(e){
    var mobile = app.globalData.userInerInfo.number
    var id = app.globalData.userInerInfo.id
    console.log(mobile)

    console.log(e)
    wx.navigateTo({
      url: '/pages/A/generate/generate?id=' + this.data.content.name + "&img=" + this.data.content.qrcode,
    })
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (data) {
    var that = this
    api.userdetails(data, res => {
      console.log(res)
      if (res.data.result.region == null){
        that.setData({
          region:[]
        })
      }else{
        var result = []
        var region = []
        result = res.data.result.region.split("/")
        console.log(result)
        if (result[0] != ""){
          region = result[0].split(" ")
        }
        res.data.result.address = result[1]
        that.setData({
          region: region
        })
      }
      that.setData({
        content: res.data.result,
        head:res.data.result.img
      })
    })
  },

  btn:function(res){
    var that = this
    var data = {}
    data.id = app.globalData.userInerInfo.id
    data.region = that.data.region.join(" ") + "/" + that.data.content.address
    data.mobile = app.globalData.userInerInfo.number
    if(that.data.head != this.data.content.img){
      data.type = 1
      console.log(that.data.head)
      wx.uploadFile({
        url: 'https://www.900yeah.com/api/dblong/updateuser',
        filePath: that.data.head,
        name: 'img',
        formData: data,
        success(res) {
          console.log(res)
          app.globalData.userInerInfo.img = that.data.head
          wx.navigateBack({
            delta:1
          })
        }
      })
    }else{
      console.log(data)
      api.updateuser(data, res => {
        if (res.data.description == 200){
          wx.navigateBack({
            delta: 1
          })
        }
      })
    }  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})